// pages/reservation/availableRez/index.js

import { apiFindPersonalRez, apiCancelRez } from '@/api/reservation'
import { useErrMessage, useInfoMessage } from '@/utils/message'

Page({
  data: {
    availableRez: [],
    page: 1,
    pageSize: 10,
    total: 0,
    hasMore: false,
  },


  async FindPersonalRez() {
    try {
      const res = await apiFindPersonalRez({
        page: 1,
        pageSize: 100,
        status: 1,
      });


      if (res.code === 200 && res.data.reservations.length > 0) {
        const availableRez = res.data.reservations

        this.setData({ availableRez });
      }

    } catch (error) {
      useErrMessage(this, '获取预约数据失败，请重试');
    }
  },

  async onLoad(options) {
    await this.FindPersonalRez();
  },


  onReachBottom() {
    // 防抖处理
    if (!this.data.hasMore) {
      return;
    }
    setTimeout(() => {
      this.setData({
        page: this.data.page + 1,
      })
      this.FindPersonalRez();
    }, 500);
  },

  // 处理取消预约
  handleCancelReservation(e) {
    const id = e.detail.id;
    apiCancelRez(id).then(res => {
      if (res.code === 200) {
        useInfoMessage(this, '取消预约成功')
        wx.redirectTo({
          url: '/pages/reservation/availableRez/index',
        })
      }
    })
  }
})